package com.renfei.multimediatask.ui.task2;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;

import com.renfei.multimediatask.R;
import com.renfei.multimediatask.base.BaseActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 音频采集与播放
 */
public class AudioGatherAndPlayActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {

    static final String[] PERMISSIONS = new String[]{
            Manifest.permission.RECORD_AUDIO
    };
    private static final int PERMISSIONS_RECORD_CAMERA = 1001;

    @BindView(R.id.btn_record_audio)
    Button btnRecordAudio;
    @BindView(R.id.btn_play_audio)
    Button btnPlayAudio;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_audio_gather_and_play;
    }

    @Override
    protected void initListener() {

    }


    @OnClick({R.id.btn_record_audio, R.id.btn_play_audio})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_record_audio:

                checkPreviewPermission();
//                startActivity(RecordPcmDataActivity.class);
                break;
            case R.id.btn_play_audio:
                startActivity(PlayPcmDataActivity.class);
                break;
        }
    }



    private void checkPreviewPermission() {
        if (EasyPermissions.hasPermissions(this, PERMISSIONS)) {
            startPreviewPage();
        } else {
            applyPermission();
        }
    }

    private void applyPermission() {
        EasyPermissions.requestPermissions(this, "申请摄像头权限", PERMISSIONS_RECORD_CAMERA, PERMISSIONS);
    }

    private void startPreviewPage() {
        startActivity(RecordPcmDataActivity.class);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PERMISSIONS_RECORD_CAMERA) {
            startPreviewPage();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        if (requestCode == PERMISSIONS_RECORD_CAMERA) {
            showToast("需要获取摄像头权限,否则无法使用");
        }
    }

}
